﻿#region File Header
/***********************************************************************
 * Copyright © 2013 Beals Software
 * All Rights Reserved
************************************************************************
Author: Donald Beals
Date: February 4th, 2013
Description: TODO: Write a description of this file here.
****************************** Change Log ******************************
02.04.13 - Created initial file. (dbeals)
***********************************************************************/
#endregion

#region Using Statements
using System;
using Newtonsoft.Json;
#endregion

namespace GameJolt.Requests
{
	/// <summary>
	/// 
	/// </summary>
	internal static class DataStoreRequestFactory
	{
		[JsonObject]
		internal sealed class DataStoreKeysResult : IJsonResult<DataStore[]>
		{
			IJsonResponse<DataStore[]> IJsonResult<DataStore[]>.Response
			{
				get
				{
					return Response;
				}
			}

			[JsonProperty("response")]
			public DataStoreKeysResponse Response
			{
				get;
				set;
			}
		}

		[JsonObject]
		internal sealed class DataStoreKeysResponse : IJsonResponse<DataStore[]>
		{
			DataStore[] IJsonResponse<DataStore[]>.ResultSet
			{
				get
				{
					return Keys;
				}
			}

			[JsonProperty("success")]
			public string WasSuccessful
			{
				get;
				set;
			}

			[JsonProperty("message")]
			public string Message
			{
				get;
				set;
			}

			[JsonProperty("keys")]
			public DataStore[] Keys
			{
				get;
				set;
			}
		}

		public static JsonRequest<DataStore[], DataStoreKeysResult> CreateGetRequest(string gameID, string publicKey, string username, string userToken, DataStoreType type)
		{
			var output = new JsonRequest<DataStore[], DataStoreKeysResult>(gameID, publicKey, Constants.DataStoreUrls.GetKeysUrl);
			if(type == DataStoreType.User)
			{
				if(string.IsNullOrWhiteSpace(username))
					throw new ArgumentException("You must specify a username when retrieving user keys.");
				if(string.IsNullOrWhiteSpace(userToken))
					throw new ArgumentException("You must specify a user token when retrieving user keys.");
				output.Parameters.Add(RequestParameter.CreateUsername(username));
				output.Parameters.Add(RequestParameter.CreateUserToken(userToken));
			}
			return output;
		}

		public static DumpRequest CreateGetRequest(string gameID, string publicKey, string username, string userToken, string key)
		{
			var output = new DumpRequest(gameID, publicKey, Constants.DataStoreUrls.GetUrl);
			output.Parameters.Add(RequestParameter.CreateKey(key));
			if(!string.IsNullOrWhiteSpace(username))
			{
				if(string.IsNullOrWhiteSpace(userToken))
					throw new ArgumentException("You must specify a user token when retrieving user keys.");
				output.Parameters.Add(RequestParameter.CreateUsername(username));
				output.Parameters.Add(RequestParameter.CreateUserToken(userToken));
			}
			return output;
		}

		public static StringRequest CreateSetRequest(string gameID, string publicKey, string username, string userToken, string key, string data)
		{
			var output = new StringRequest(gameID, publicKey, Constants.DataStoreUrls.SetUrl, RequestParameter.CreateKey(key), RequestParameter.CreateData(data));
			if(!string.IsNullOrWhiteSpace(username))
			{
				if(string.IsNullOrWhiteSpace(userToken))
					throw new ArgumentException("You must specify a user token when retrieving user keys.");
				output.Parameters.Add(RequestParameter.CreateUsername(username));
				output.Parameters.Add(RequestParameter.CreateUserToken(userToken));
			}
			return output;
		}

		public static DumpRequest CreateUpdateRequest(string gameID, string publicKey, string username, string userToken, string key, DataStoreOperation operation, string value)
		{
			var output = new DumpRequest(gameID, publicKey, Constants.DataStoreUrls.UpdateUrl, RequestParameter.CreateKey(key), RequestParameter.CreateOperation(operation), RequestParameter.CreateValue(value));
			if(!string.IsNullOrWhiteSpace(username))
			{
				if(string.IsNullOrWhiteSpace(userToken))
					throw new ArgumentException("You must specify a user token when retrieving user keys.");
				output.Parameters.Add(RequestParameter.CreateUsername(username));
				output.Parameters.Add(RequestParameter.CreateUserToken(userToken));
			}
			return output;
		}

		public static StringRequest CreateRemoveRequest(string gameID, string publicKey, string username, string userToken, string key)
		{
			var output = new StringRequest(gameID, publicKey, Constants.DataStoreUrls.RemoveUrl, RequestParameter.CreateKey(key));
			if(!string.IsNullOrWhiteSpace(username))
			{
				if(string.IsNullOrWhiteSpace(userToken))
					throw new ArgumentException("You must specify a user token when retrieving user keys.");
				output.Parameters.Add(RequestParameter.CreateUsername(username));
				output.Parameters.Add(RequestParameter.CreateUserToken(userToken));
			}
			return output;
		}
	}
}
